#include "gm3d.h"

void GM3D::AddTiltedBlock(modelist para_list){
	double xs_1,xe_1,xmin_1,xmax_1;
	double xs_2,xe_2,xmin_2,xmax_2;
	double ys_1,ye_1,ymin_1,ymax_1;
	double ys_2,ye_2,ymin_2,ymax_2;
	double layer_xmin,layer_xmax,layer_ymin,layer_ymax;
	double zs,ze,zmin,zmax;
	bool model_added = false;

	std::stringstream tmp_ss;
	gctl::str2ss(para_list.mod_para, tmp_ss, "/");
	tmp_ss >> xs_1 >> xe_1 >> ys_1 >> ye_1 >> zs 
		>> xs_2 >> xe_2 >> ys_2 >> ye_2 >> ze;

	if (tmp_ss.fail())
		throw GM3D_INVALID_MODEL_PARAMETER;

	xmin_1 = GCTL_MIN(xs_1,xe_1); xmax_1 = GCTL_MAX(xs_1,xe_1);
	ymin_1 = GCTL_MIN(ys_1,ye_1); ymax_1 = GCTL_MAX(ys_1,ye_1);
	xmin_2 = GCTL_MIN(xs_2,xe_2); xmax_2 = GCTL_MAX(xs_2,xe_2);
	ymin_2 = GCTL_MIN(ys_2,ye_2); ymax_2 = GCTL_MAX(ys_2,ye_2);
	zmin = GCTL_MIN(zs,ze); zmax = GCTL_MAX(zs,ze);

	if (para_list.val_type == "replace")
	{
		for (int i = 0; i < model_num_; i++){
			//计算当前层的x与y范围
			layer_xmin = (model_cube_[i].cen.z - zmin)*(xmin_2 - xmin_1)/(zmax - zmin) + xmin_1;
			layer_xmax = (model_cube_[i].cen.z - zmin)*(xmax_2 - xmax_1)/(zmax - zmin) + xmax_1;
			layer_ymin = (model_cube_[i].cen.z - zmin)*(ymin_2 - ymin_1)/(zmax - zmin) + ymin_1;
			layer_ymax = (model_cube_[i].cen.z - zmin)*(ymax_2 - ymax_1)/(zmax - zmin) + ymax_1;

			if (model_cube_[i].cen.x >= layer_xmin && model_cube_[i].cen.x <= layer_xmax &&
				model_cube_[i].cen.y >= layer_ymin && model_cube_[i].cen.y <= layer_ymax &&
				model_cube_[i].cen.z >= zmin && model_cube_[i].cen.z <= zmax){
				model_block_val_[i] = para_list.mod_value; //注意重复赋值的块体会覆盖
				model_added = true;
			}
		}
	}
	else if (para_list.val_type == "add")
	{
		for (int i = 0; i < model_num_; i++){
			//计算当前层的x与y范围
			layer_xmin = (model_cube_[i].cen.z - zmin)*(xmin_2 - xmin_1)/(zmax - zmin) + xmin_1;
			layer_xmax = (model_cube_[i].cen.z - zmin)*(xmax_2 - xmax_1)/(zmax - zmin) + xmax_1;
			layer_ymin = (model_cube_[i].cen.z - zmin)*(ymin_2 - ymin_1)/(zmax - zmin) + ymin_1;
			layer_ymax = (model_cube_[i].cen.z - zmin)*(ymax_2 - ymax_1)/(zmax - zmin) + ymax_1;

			if (model_cube_[i].cen.x >= layer_xmin && model_cube_[i].cen.x <= layer_xmax &&
				model_cube_[i].cen.y >= layer_ymin && model_cube_[i].cen.y <= layer_ymax &&
				model_cube_[i].cen.z >= zmin && model_cube_[i].cen.z <= zmax){
				if (model_block_val_[i] == GCTL_BDL_MAX)
					model_block_val_[i] = para_list.mod_value; //注意重复赋值的块体会覆盖
				else
					model_block_val_[i] += para_list.mod_value;
				model_added = true;
			}
		}
	}
	else if (para_list.val_type == "erase")
	{
		for (int i = 0; i < model_num_; i++){
			//计算当前层的x与y范围
			layer_xmin = (model_cube_[i].cen.z - zmin)*(xmin_2 - xmin_1)/(zmax - zmin) + xmin_1;
			layer_xmax = (model_cube_[i].cen.z - zmin)*(xmax_2 - xmax_1)/(zmax - zmin) + xmax_1;
			layer_ymin = (model_cube_[i].cen.z - zmin)*(ymin_2 - ymin_1)/(zmax - zmin) + ymin_1;
			layer_ymax = (model_cube_[i].cen.z - zmin)*(ymax_2 - ymax_1)/(zmax - zmin) + ymax_1;

			if (model_cube_[i].cen.x >= layer_xmin && model_cube_[i].cen.x <= layer_xmax &&
				model_cube_[i].cen.y >= layer_ymin && model_cube_[i].cen.y <= layer_ymax &&
				model_cube_[i].cen.z >= zmin && model_cube_[i].cen.z <= zmax){
				model_block_val_[i] = GCTL_BDL_MAX; //注意重复赋值的块体会覆盖
				model_added = true;
			}
		}
	}
	else throw GM3D_INVALID_MODEL_VAL_TYPE;

	if (!model_added)
		throw GM3D_NOMODEL_ADDED;
	return;
}